package com.zhixiang.xaec.action.system;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.zhixiang.core.command.QueryFilter;
import com.zhixiang.core.entity.TreeObj;
import com.zhixiang.core.util.String2Array;
import com.zhixiang.core.web.action.BaseAction;
import com.zhixiang.xaec.entity.system.DicType;
import com.zhixiang.xaec.service.system.DicTypeService;


@Controller
@Scope("prototype")
public class DicTypeAction extends BaseAction
{

    private static final long serialVersionUID = 5112640890674495582L;

    @Autowired
    private DicTypeService dicTypeService;

    private DicType dicType;

    public String save()
    {
        dicTypeService.save(dicType);
        return SUCCESS;
    }

    public String delete()
    {
        try
        {
            String ids = getRequest().getParameter("ids");
            Long[] longs = String2Array.stringToLongArray(ids);
            dicTypeService.remove(longs);
        }
        catch (Exception e)
        {
            setSuccess(false);
            setMsg("删除字典分类失败");
        }

        return SUCCESS;
    }

    public String get()
    {    
        String id= getRequest().getParameter("id");
        if(id!=null&& !"".equals(id)){
            setResult(dicTypeService.get(Long.valueOf(id)));
        }
        
        return SUCCESS;
    }

    /**
     * Description: 直接向页面输出字符串
     * 
     * @see
     */
    public void getTree()
    {
        QueryFilter localQueryFilter = new QueryFilter(getRequest());
        String isPage=getRequest().getParameter("isPage");
        if("0".equals(isPage)){
            //isPage只要不为空，就不分页
            localQueryFilter.setIsPage(0);
        }
        List<DicType> localList = dicTypeService.getAll(localQueryFilter);
        List<TreeObj> treelist = new ArrayList<TreeObj>();
        for (DicType t : localList)
        {
            TreeObj obj = new TreeObj();
            obj.setId(t.getId().toString());
            obj.setText(t.getDic());
            obj.setLeaf(true);
            treelist.add(obj);
        }
        // 直接向页面输出字符串
        outJsonString(new Gson().toJson(treelist));
    }

    public DicType getDicType()
    {
        return dicType;
    }

    public void setDicType(DicType dicType)
    {
        this.dicType = dicType;
    }
}
